// *** LICENSE HEADER ***
// Filename: ./canvas.cpp
// This file is part of Nodewarz 0.1
// Nodewarz is a 3d OpenGL strategy game
// Copyright (C) 2007 CZestmyr
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// *** END LICENSE HEADER ***
#include "canvas.h"

GLuint canvas::texture = 0;

canvas::canvas(int newX, int newY, int newW, int newH, int id):
    guielement(newX, newY, "", id){
    w = newW;
    h = newH;
    x = newX;
    y = newY;
}

canvas::~canvas() {}

int canvas::Click(int clickX, int clickY){ return 0; }

int canvas::Release(int clickX, int clickY){ return 0; }

void canvas::Draw(fonts* fontDrawer){
    glDisable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);

    //Calculate texture coordinates
    float coordx, coordy;
    coordx = (float)w/_CANVAS_TSX;
    coordy = (float)h/_CANVAS_TSY;

    glBegin(GL_TRIANGLES);
    glColor3ub(r,g,b);
        glTexCoord2f(0.0f, 0.0f); glVertex2i(x, _HEIGHT-y);
        glTexCoord2f(coordx, 0.0f); glVertex2i(x+w, _HEIGHT-y);
        glTexCoord2f(0.0f, coordy); glVertex2i(x, _HEIGHT-y-h);
        glTexCoord2f(coordx, 0.0f); glVertex2i(x+w, _HEIGHT-y);
        glTexCoord2f(coordx, coordy); glVertex2i(x+w, _HEIGHT-y-h);
        glTexCoord2f(0.0f, coordy); glVertex2i(x, _HEIGHT-y-h);
    glEnd();
}

void canvas::LoadTexture(const char* filename){
    texture = ::LoadTexture(filename);
}

void canvas::UnloadTexture(){
    ::UnloadTexture(texture);
}
